package edu.washington.cs.workflow.analysis.guistructure;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

import org.uispec4j.Panel;

import edu.washington.cs.workflow.util.Globals;
import edu.washington.cs.workflow.util.Utils;

public class ExceptionalExecution implements SequenceExecutionOutcome {

	public final Throwable e;
	public final Collection<Panel> activePanels;
	
	public ExceptionalExecution(Throwable e) {
		this(e, Collections.<Panel>emptyList());
	}
	
	//only used in replay
	public static ExceptionalExecution makeExceptionalExecution(Throwable e, Collection<Panel> panels) {
		return new ExceptionalExecution(e, panels);
	}
	
	private ExceptionalExecution(Throwable e, Collection<Panel> panels) {
		Utils.checkNotNull(e);
		Utils.checkNotNull(panels);
		this.e = e;
		activePanels = new LinkedList<Panel>();
		activePanels.addAll(panels);
	}
	
	@Override
	public String getOutcome() {
		StringBuilder sb = new StringBuilder();
		for(StackTraceElement elem : e.getStackTrace()) {
			sb.append("  " + elem);
			sb.append(Globals.lineSep);
		}
		return "Trigger exception: " + Globals.lineSep + sb.toString();
	}

	@Override
	public Collection<Panel> getActivePanels() {
		return activePanels;
	}

	@Override
	public Throwable getException() {
		return e;
	}

}
